package com.tenebrous.jestic.controller.framwork.system.meta;

import com.tenebrous.jestic.compents.annotations.Weblog;
import com.tenebrous.jestic.compents.annotations.WeblogEnum;
import com.tenebrous.jestic.controller.SupperController;
import com.tenebrous.jestic.entity.framwork.meber.organ.OrgUser;
import com.tenebrous.jestic.entity.framwork.system.MetaDistrict;
import com.tenebrous.jestic.service.framwork.system.MetaDistrictService;
import com.rain.ops.sdk.dto.ApiResult;
import com.rain.ops.sdk.utils.UUIDUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * 行政区划
 */
@RestController
@RequestMapping("/meta/district")
public class DistrictController extends SupperController {

    @Autowired
    private MetaDistrictService districtService;

    /**
     * 主键查询id
     * chengb
     *
     * @param
     * @return
     */
    @PostMapping("/get")
    public ApiResult get(@NotBlank String id) {
        MetaDistrict item = districtService.selectByPrimaryKey(id);
        if (item != null) {
            return new ApiResult(200, "查询成功。", item);
        }
        return new ApiResult(999, "查询失败，未找到指定对象。");
    }

    /**
     * 查询目录树
     *
     * @return
     */
    @PostMapping("/query/pid")
    public ApiResult queryByPid(@NotBlank String pid) {
        return new ApiResult(200, "查询成功。", districtService.queryByPid(pid));
    }

    /**
     * 根据code查询子节点
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/query/code")
    public ApiResult queryByCode(@NotBlank String code) {
        return new ApiResult(200, "查询成功。", districtService.queryByCode(code));
    }

    /**
     * 保存或更新对象
     * chengb
     *
     * @param item
     * @return
     */
    @PostMapping("/supdate")
    @Weblog(description = "更新行政区划记录", method = WeblogEnum.UPDATE)
    public ApiResult supdate(@NotNull MetaDistrict item) {
        OrgUser user = super.getCurrentUser();
        if (StringUtils.isNotBlank(item.getId())) { // 修改
            item.setUpdateDate(new Date());
            item.setUpdateUserid(user.getId());
            districtService.updateSelective(item);
        } else { // 新增
            item.setId(UUIDUtils.randomUUID());
            item.setCreateDate(new Date());
            item.setCreateUserid(user.getId());
            item.setSort(System.currentTimeMillis());
            item.setDstate(1);
            districtService.saveSelective(item);
        }
        return new ApiResult(200, "更新成功。");
    }

    /**
     * 删除对象
     *
     * @param id
     * @return
     */
    @PostMapping("/delete")
    @Weblog(description = "删除指定行政区划记录", method = WeblogEnum.UPDATE)
    public ApiResult delete(@NotBlank String id) {
        districtService.deleteById(id);
        return new ApiResult(200, "删除成功。");
    }
}
